package com.zzh.zzhaiagent.tools;

import cn.hutool.core.io.FileUtil;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import static com.zzh.zzhaiagent.constant.FileConstant.FILE_SAVE_PATH;

/**
 * @author: zzh
 * @date: 2025/05/16 14:36:42
 * @version: 1.0
 */
//当大模型需要进行读取文件或者写入新文件的时候，就需要用到该工具类 （比如生成图片调用该工具类等等）
@Component
public class FileOperationTool {
    private final String FILE_DIR = FILE_SAVE_PATH + "/file";

    /**
     * 读取文件工具method
     * @param fileName 文件名
     * @return 返回给AI大模型回答
     */
    @Tool(description = "read content from file")
    public String readFile(@ToolParam(description = "Name of the file to read") String fileName) {
       //创建文件的路径
       String path = FILE_DIR + "/" + fileName;
       try {
           return FileUtil.readUtf8String(path);
       }catch (Exception e){
           return "Error reading file: " + e.getMessage();
       }
    }

    /**
     * 写入文件工具method
     * @param fileName 文件名
     * @param content 写入的内容
     * @return 返回给AI大模型回答
     */
    @Tool(description = "write content to file")
    public String writeFile(@ToolParam(description = "Name of the file to write") String fileName
            ,@ToolParam(description = "Content to write to the file") String content) {
        //创建文件的路径
        String path = FILE_DIR + "/" + fileName;
        try{
            FileUtil.mkdir(FILE_DIR);//创建文件夹
            FileUtil.writeUtf8String(content, path);
            return "File saved successfully to :" + path;
        }catch (Exception e){
            return "Error writing to file: " + e.getMessage();
        }
    }

}
